<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
    var badreport_id = '{{ .m.Id }}';
    var purchase_num = 0;
    var tables = $('#dataTables').DataTable({
        "sAjaxSource": '{{ urlfor "InventoryController.GetInventoryProduct"}}'
        , "bLengthChange": false
        , "fnServerData": function (sSource, aoData, fnCallback) {
            var queryParams = checkQueryTableData(aoData);
            queryParams.typeid = defErpTypeid;
            queryParams.isnegative = '1';
            var form = $('#queryForm'+defErpTypeid).serializeArray();
            $.each(form, function(i, value){
                queryParams[value.name] = value.value;
            });
            $.post(sSource, JSON.stringify(queryParams), function (resp) {
                fnCallback(resp);
            }, 'json')
        }
        , columns: [
            {"data": "Id", "bSortable": false}
            ,{render: function (data, type, full, meta) {
                    var html = productGroupName(full.Products);
                    html += '<div class="attr">';
                    html += goodsAttr(defErpTypeid, full);
                    html += '<span>库存数量：'+ full.purchase_num +'</span>';
                    html += '</div>';
                    return html;
            },"bSortable": false}
            ,{
                "mDataProp": "Action",
                "bSortable": false,
                "sWidth": "10%",
                "render": function (data, type, full, meta) {
                    btns = '<a href="javascript:;" style="font-size:18px;" class="open-dialog-btn" data-purchase="'+ full.purchase_num +'" data-entryid="{{.m.Id}}" data-invid="' + full.Id + '"><i class="am-icon-shopping-cart"></i></a>';
                    return btns;
                }
            }
        ]
        , "aLengthMenu": [20, 50, 100, 200]
		,'bLengthChange': true
        , "serverSide": true
        , "bPaginate": true
        , "bFilter": false	//是否启动过滤、搜索功能
        , "bInfo": true //是否显示页脚信息，DataTables插件左下角显示记录数
        , "bProcessing": true //DataTables载入数据时，是否显示‘进度’提示
        , "drawCallback": function (settings) {

            $('.open-dialog-btn').click(function () {
                var entry_id = $(this).data('entryid');
                var inv_id = $(this).data('invid');
                purchase_num = $(this).data('purchase');
                var url = '{{urlfor "BadreportController.BadreportAddToGoods"}}?entry_id='+entry_id+'&inv_id='+inv_id;
                layer.open({
                    type: 2,
                    title: '添加报损商品',
                    shadeClose: false,
                    shade: 0.2,
                    maxmin: false,
                    shift: 1,
                    area: ['620px', '310px'],
                    content: url
                });
                return false;
            });
        }
    });
    function goodsAttr(typeid, full) {
        var html = '';
        switch(typeid){
            case 10:
                html += '<span>'+ full.Snd.sph +'</span><span>'+ full.Snd.cyl +'</span>';
                break;
            case 20:
                html += '<span>'+ full.Snd.sph +'</span><span>'+ full.Snd.cyl +'</span>';
                html += '<span>批号：'+ full.Acceptance.lot_number +'</span><span>生产日期：'+ timetransDate(full.Acceptance.manufacture_date) +'</span><span>有效日期：'+ timetransDate(full.Acceptance.expiry_date) +'</span>';
                break;
            case 30:
                html += '<span>'+ full.Snd.model +'</span><span>'+ full.Snd.color +'</span><span>'+ full.Snd.genre +'</span>';
                break;
            case 40:
                html += '<span>'+ full.Snd.sph +'</span><span>'+ full.Snd.add +'</span>';
                break;
            case 41:
                html += '<span>'+ full.Snd.model +'</span><span>'+ full.Snd.color +'</span><span>'+ full.Snd.genre +'</span>';
                break;
            case 50:
                html += '<span>批号：'+ full.Acceptance.lot_number +'</span><span>生产日期：'+ timetransDate(full.Acceptance.manufacture_date) +'</span><span>有效日期：'+ timetransDate(full.Acceptance.expiry_date) +'</span>';
                break;
        }
        return html;
    }
    $('#cennelBtn').click(function () {
        $.post('{{ urlfor "BadreportController.BadreportDelete" }}', {'id': badreport_id}, function (res) {
            if (res.code == 0) {
                location = '{{ urlfor "BadreportController.Badreport" }}';
            }
        });
    });

    $(".query-submit").bind("click", function () { //点击按钮 触发table重新请求服务器
        $("#dataTables").dataTable().fnDraw(false);
        return false;
    });
    //类别切换事件
    function defErpTypeidEvent(obj) {
        defErpTypeid= $(obj).data('id');
        $("#dataTables").dataTable().fnDraw(false);
        $('.query-form').hide();
        $('#queryForm'+defErpTypeid).show();
    }
    //回调
    function addtogoodsCallBack() {
        layer.closeAll();
        badreportGetToGoods();
    }
    badreportGetToGoods();
    function badreportGetToGoods() {
        $.get('{{  urlfor "BadreportController.BadreportGetToGoods" }}', {'badreport_id':badreport_id}, function (res) {
            if(res.code == 0) {
                $('#goods-list').html('');
                $.each(res.data, function(index, value){
                    var html = '<div class="list">\
                        <p class="am-text-sm" style="color: #000">'+ productGroupName(value.Inventory.Products) +'</p>\
                        <p style="font-size: 12px;">'+ goodsAttr(value.Inventory.Products.TypeId, value.Inventory) +'</p>\
                        <p style="font-size: 12px;">报损数量：'+ value.badreport_num +' 原因：'+ value.badreport_reson +' <a href="#" onclick="deleteData('+ value.Id +', this);return false;" class="am-text-danger am-text-xs">删除</a></p>\
                    </div>';

                    $('#goods-list').append(html);
                });
            }
            console.log(res);
        });
    }

    //删除明细
    function deleteData(id, h) {
        $.post('{{  urlfor "BadreportController.BadreportGetToGoods" }}', {'badreport_data_id':id, 'badreport_id':badreport_id}, function (res) {
                if(res.code == 0) {
                    $(h).parent().parent().remove();return;
                } else {
                    layer.alert(res.msg); return ;
                }
        }, 'json');
    }
    //提交报损单
    $('#submitOrder').click(function () {
        $.post('{{ urlfor "BadreportController.BadreportAdd" }}/'+badreport_id, function(res){
            if(res.code == 1) {
                layer.alert(res.msg);return false
            }
            location = '{{ urlfor "BadreportController.Badreport" }}';
        }, 'json');
        return false;
    });

    $.post('{{ urlfor "ProductsController.GetOptions"}}', {'fields':['goods_lens_genre', 'goods_lens_optical_design', 'goods_lens_refractivity', 'goods_function_info', 'goods_lens_material_quality']},  function(res){
        setattroption('genre',res.data.goods_lens_genre);
        setattroption('optical_design',res.data.goods_lens_optical_design);
        setattroption('refractivity',res.data.goods_lens_refractivity);
        setattroption('function_info',res.data.goods_function_info);
        setattroption('material_quality',res.data.goods_lens_material_quality);
    });
    function setattroption(id,data) {
        var option = '<option value=""></option>';
        $.each(data, function(index, value) {
            option += '<option value="'+ value.val +'">'+ value.val +'</option>';
        });
        $('#'+id).html(option);
    }
</script>
